package com.qs.controller;


import com.qs.model.dto.CartDto;
import com.qs.model.entity.Cart;
import com.qs.response.Response;
import com.qs.service.ICartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-03-29
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequestMapping("/cart")
@RequiredArgsConstructor
public class CartController {

    private final ICartService cartService;

    /**
     * 删除购物车数据
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Response remove(@PathVariable("id") Integer id){
        log.info("id为{}的购物车数据移除请求....",id);
        cartService.removeById(id);
        log.info("id为{}的购物车数据移除请求处理成功！",id);
        return Response.success();
    }

    /**
     * 添加商品到购物车
     * @param cart
     * @return
     */
    @PostMapping("/add")
    public Response addCart(@RequestBody Cart cart){
        log.info("id为{}的用户添加id为{}的商品到购物车请求....",cart.getUserId(),cart.getGoodsId());
        cartService.addCart(cart);
        log.info("id为{}的用户添加id为{}的商品到购物车请求处理成功！",cart.getUserId(),cart.getGoodsId());
        return Response.success();
    }

    /**
     * 修改购物车中原有的商品数量
     * @param cart
     * @return
     */
    @PostMapping("/update")
    public Response updateCart(@RequestBody Cart cart){
        log.info("id为{}的用户修改id为{}的商品到购物车请求....",cart.getUserId(),cart.getGoodsId());
        cartService.updateCart(cart);
        log.info("id为{}的用户修改id为{}的商品到购物车请求处理成功！",cart.getUserId(),cart.getGoodsId());
        return Response.success();
    }

    /**
     * 查询当前用户的购物车数据
     * @param userId
     * @return
     */
    @GetMapping("/query/{userId}")
    public Response query(@PathVariable("userId") Integer userId){
        log.info("id为{}的用户正在进行购物车数据查询请求....",userId);
        List<CartDto> list = cartService.queryCartData(userId);
        log.info("id为{}的用户正在进行购物车数据查询请求处理成功！",userId);
        return Response.success(list);
    }

}
